package ma.meditel.reporting.metier.implementations;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ma.meditel.reporting.dao.interfaces.IInterfaceDao;
import ma.meditel.reporting.dao.interfaces.ITrafficDao;
import ma.meditel.reporting.dao.interfaces.ITrafficNotificationDao;
import ma.meditel.reporting.entities.Interface;
import ma.meditel.reporting.entities.Traffic;
import ma.meditel.reporting.entities.TrafficNotification;
import ma.meditel.reporting.metier.interfaces.ITrafficNotificationMetier;
import ma.meditel.reporting.util.HelperApp;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class TrafficNotificationMetierImpl implements ITrafficNotificationMetier {
	
	private ITrafficNotificationDao dao;
	private ITrafficDao daoTraffic;
	private IInterfaceDao daoInterface;

	public ITrafficDao getDaoTraffic() {
		return daoTraffic;
	}

	public void setDaoTraffic(ITrafficDao daoTraffic) {
		this.daoTraffic = daoTraffic;
	}

	public IInterfaceDao getDaoInterface() {
		return daoInterface;
	}

	public void setDaoInterface(IInterfaceDao daoInterface) {
		this.daoInterface = daoInterface;
	}

	public ITrafficNotificationDao getDao() {
		return dao;
	}

	public void setDao(ITrafficNotificationDao dao) {
		this.dao = dao;
	}

	@Override
	public TrafficNotification getTrafficNotification(Long idTrafficNotification) {
		return dao.getTrafficNotification(idTrafficNotification);
	}

	@Override
	public List<TrafficNotification> getAllTrafficNotification() {
		
		return dao.getAllTrafficNotification();
	}

	@Override
	public void mergeTrafficNoti(TrafficNotification trafficNoti) {
		dao.mergeTrafficNoti(trafficNoti);
		
	}

	@Override
	public void addNoti(TrafficNotification trafficNoti) throws Exception {
		dao.addNoti(trafficNoti);
		
	}

	@Override
	public void removeNoti(Long idTrafficNoti) throws Exception {
		dao.removeNoti(idTrafficNoti);
		
	}

	@Override
	public void removeAllTrafficNotis() throws Exception {
		dao.removeAllTrafficNotis();
	}

	@Override
	public TrafficNotification getTrafficNotificationWithoutId(
			Long idInterface, Date jourDate, String typeTraffic) {
		return dao.getTrafficNotificationWithoutId(idInterface, jourDate, typeTraffic);
	}

	@Override
	public Boolean getSaturationTest(ArrayList<Double> listValues__, double rang2, int varScore) {
		HelperApp app = new HelperApp();
//		Double rang = app.rang(listValues__);
//		System.out.println("the rang : "+rang);
		return app.saturationExist(listValues__,rang2,varScore);
	}

	@Override
	public Date getMaxDateTrafficNoti() {
		return dao.getMaxDateTrafficNoti();
	}

	@Override
	public ArrayList<Double> listOfTrafficToDouble(List<Traffic> uneListTraffic) {
		
		ArrayList<Double> list = new ArrayList<Double>();
		
		for(Traffic t : uneListTraffic)
		{
			list.add(t.getCalculatedMetriceValue());
		}
		
		return list;
	}

	@Override
	public Boolean insertSaturationOfTrafficInOutForOneDay(Date dateOfDay,
			Long idInterface, double rang, double trafficMin, double trafficMax, int varScore) throws Exception {
		
		Boolean test = false;
		TrafficMetierImpl trafficMetier = new TrafficMetierImpl();
		List<Traffic> traffics = daoTraffic.getAllInterfaceTrafficsOfTheDay(idInterface, dateOfDay, Traffic.TRAFFIC_OUT);
		
		if(traffics != null && traffics.size() > 0)
		{
			
			Double val1 = daoTraffic.getMaxValueTrafficInADay(dateOfDay, idInterface, Traffic.TRAFFIC_OUT);
			val1 = (new HelperApp()).getCalculatedMetriceValue(val1);
			if(val1 > trafficMin && val1 < trafficMax)
			{
				System.out.println("Val 1 : "+val1);
				List<Traffic> trafficsOfDay = trafficMetier.getTrafficByHourForOnrDay(traffics);
				
				
				if(getSaturationTest(listOfTrafficToDouble(trafficsOfDay),rang, varScore))
				{
					System.out.println("stauration detected for OUT -> interface : " + idInterface+"\t date : "+dateOfDay);
					TrafficNotification trafficNottification = new TrafficNotification();
					trafficNottification.setDateJour(dateOfDay);
					trafficNottification.setIinterface(daoInterface.getInterface(idInterface));
					trafficNottification.setNotificationVu(0);
					trafficNottification.setTypeOrClassTraffic(Traffic.TRAFFIC_OUT);
					
					if(getTrafficNotificationWithoutId(trafficNottification.getIdInterface(), trafficNottification.getDateJour(), trafficNottification.getTypeOrClassTraffic()) == null)
					{
						dao.addNoti(trafficNottification);
					}
					
				}
				
				test = true;
			}
			
		}
		
		
		traffics = daoTraffic.getAllInterfaceTrafficsOfTheDay(idInterface, dateOfDay, Traffic.TRAFFIC_IN);
		if(traffics != null && traffics.size() > 0)
		{
			Double val2 = daoTraffic.getMaxValueTrafficInADay(dateOfDay, idInterface, Traffic.TRAFFIC_IN);
			val2 = (new HelperApp()).getCalculatedMetriceValue(val2);
			if(val2 > trafficMin && val2 < trafficMax)
			{
				System.out.println("Val 2 : "+val2);
				List<Traffic> trafficsOfDay = trafficMetier.getTrafficByHourForOnrDay(traffics);
			
				if(getSaturationTest(listOfTrafficToDouble(trafficsOfDay), rang, varScore))
				{
					System.out.println("stauration detected for IN -> interface : " + idInterface+"\t date : "+dateOfDay);
					TrafficNotification trafficNottification = new TrafficNotification();
					trafficNottification.setDateJour(dateOfDay);
					trafficNottification.setIinterface(daoInterface.getInterface(idInterface));
					trafficNottification.setNotificationVu(0);
					trafficNottification.setTypeOrClassTraffic(Traffic.TRAFFIC_IN);
					
					if(getTrafficNotificationWithoutId(trafficNottification.getIdInterface(), trafficNottification.getDateJour(), trafficNottification.getTypeOrClassTraffic()) == null)
					{
						dao.addNoti(trafficNottification);
					}		
				}
				
				test = true;
			}
			
		}
		
		return test;
	}

	@Override
	public void updateNotificationTable(Date dayDate, String trafficType, double rang, double trafficMin, double trafficMax, int varScore) throws Exception {
		Date aujourdhui = new Date();
////		Date temp = getMaxDateTrafficNoti();
//		
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date temp = formatter.parse("2014-08-01 00:00:00");
//		Date dateBegin = new Date(temp.getTime());
		
		
		System.out.println(dayDate.compareTo(aujourdhui)+"\t date : "+dayDate);
//		Scanner sc = new Scanner(System.in);
//	     int i = sc.nextInt();
	     
		List<Interface> listOfInterface = daoInterface.getAllInterfaces();
		System.err.println("size liste : "+listOfInterface.size());
		for(Interface inter : listOfInterface)
		{	
			
					System.err.println("interface : "+inter.getIdInterface()+"\tdate : "+dayDate);
					
					if(trafficType.compareTo("InOut")==0)
					{
						insertSaturationOfTrafficInOutForOneDay(dayDate, inter.getIdInterface(), rang, trafficMin, trafficMax, varScore);
					}
					
					
//					dateBegin.setTime(dateBegin.getTime() + 1000*3600*24);
				
				
				/*if(daoTraffic.getAllInterfaceTrafficsOfTheDay(inter.getIdInterface(), dateBegin, Traffic.TRAFFIC_IN).size() > 1
						&& daoTraffic.getAllInterfaceTrafficsOfTheDay(inter.getIdInterface(), dateBegin, Traffic.TRAFFIC_IN).size() > 1){
					Double val1 = daoTraffic.getMaxValueTrafficInADay(dateBegin, inter.getIdInterface(), Traffic.TRAFFIC_IN);
					Double val2 = daoTraffic.getMaxValueTrafficInADay(dateBegin, inter.getIdInterface(), Traffic.TRAFFIC_OUT);
					System.out.println("val1 : "+val1+" val2 : "+val2);
					if(val1 > 5 && val2 > 5) 
						{	
							System.out.println("plus 5Mo");*/
							
				/*		}
				}*/
				
		}
		
	}

}
